package com.madou.dbtool.redis.dialog;

import com.madou.common.safe.utils.DecodeClassLoader;
import com.madou.common.safe.utils.DecodeClassLoaderUtil;
import com.madou.dbtool.common.enums.DBEnum;
import com.madou.dbtool.constans.Constans;
import com.madou.dbtool.ui.dialog.DialogResult;
import com.madou.dbtool.utils.ConfigManager;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

public class ConnectionDialog {

    /**
     * 添加内容
     * @param id
     * @param title
     * @param initValue
     */
    public static void actionRedisConnect(String id, String title, Map<String, Object> initValue){
        JDialog dialog = createMColumn(initValue,101);
        dialog.setTitle(title);
        dialog.pack();
        dialog.setLocationRelativeTo(Constans.PARENT_COMPONENT);
        dialog.setVisible(true);
        DialogResult dialogResult = (DialogResult)dialog;
        Map<String, Object> result = dialogResult.getResult();
        if(result!=null&&result.containsKey("name")&&result.containsKey("uri")){
            HashMap treeNode = (HashMap) ConfigManager.getInstance().getTreeNode(DBEnum.REDIS);
            treeNode.put(id,result);
            ConfigManager.getInstance().setTreeNode(DBEnum.REDIS,treeNode);
        }
    }

    private static JDialog createMColumn(Map<String, Object> initValue, int type){
        DecodeClassLoader decodeClassLoader = DecodeClassLoaderUtil.getDecodeClassLoader();
        try {
            Class<?> mColumn = decodeClassLoader.findClassByAlias("commonDialog", "com.madou.dbtool.ui.dialog.CommonDialog");
            return (JDialog) mColumn.getConstructors()[0].newInstance(initValue,type,"");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
